package com.ruoyi.system.controller;


import com.bw.springboot.starter.upload.model.Chunk;
import com.bw.springboot.starter.upload.service.FileService;
import com.bw.springboot.starter.upload.util.FileResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;



/**
 * @author : 赵贤
 * @create: 2023-03-10 10:52
 * @Description:
 */
@RestController
@RequestMapping("bigUpload")
@CrossOrigin
public class BigUploadController {


    @Resource
    private FileService fileService;

    /**
     * 多文件上传
     * @param
     *
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("postFileUpload")
    public Object postFileUpload(@ModelAttribute Chunk chunk) throws InterruptedException {
        fileService.postFileUpload(chunk);
        if (chunk.getTotalChunks().equals(chunk.getChunkNumber())) {
            //眠2秒
            TimeUnit.SECONDS.sleep(2);
            //合并
            FileResult fileResult = fileService.mergeFile(chunk);
            return fileResult;
        }
        return null;
    }

}
